package app.tvprogram;

import java.net.URL;
import java.rmi.RemoteException;
import java.util.Arrays;

import javax.xml.namespace.QName;

import org.apache.axis.AxisEngine;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.constants.Style;
import org.apache.axis.constants.Use;
import org.apache.axis.description.OperationDesc;
import org.apache.axis.description.ParameterDesc;
import org.apache.axis.encoding.ser.ArrayDeserializerFactory;
import org.apache.axis.encoding.ser.ArraySerializerFactory;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.encoding.ser.EnumDeserializerFactory;
import org.apache.axis.encoding.ser.EnumSerializerFactory;
import org.apache.axis.encoding.ser.SimpleDeserializerFactory;
import org.apache.axis.encoding.ser.SimpleListDeserializerFactory;
import org.apache.axis.encoding.ser.SimpleListSerializerFactory;
import org.apache.axis.encoding.ser.SimpleSerializerFactory;
import org.apache.axis.soap.SOAPConstants;
import org.apache.axis.utils.JavaUtils;

public class TvProgramWsClient extends org.apache.axis.client.Stub {
    private final String endpoint = "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx";
    private static OperationDesc[] operationDescArray;
    static {
        initOperationDesc();
    }

    public String[] invoke(String operationName, Object[] params) {
        String[] ret = null;
        try {
            Service service = new Service();
            service.setTypeMappingVersion("1.2");
            _call = (Call) service.createCall();
            // call.setMaintainSession(maintainSession);
            // call.setUsername(username);
            // call.setPassword(password);
            _call.setTargetEndpointAddress(new URL(endpoint));
            // call.setTimeout(timeout);
            QName portName = new QName("", "ChinaTVprogramWebServiceSoap");
            _call.setPortName(portName);
            _call.setEncodingStyle(null);
            setClassMapping(_call);
            for (OperationDesc od : operationDescArray) {
                if (od != null && operationName.equals(od.getName()))
                    _call.setOperation(od);
            }
            _call.setUseSOAPAction(true);
            _call.setSOAPActionURI("http://WebXml.com.cn/" + operationName);
            _call.setEncodingStyle(null);
            _call.setProperty(Call.SEND_TYPE_ATTR, Boolean.FALSE);
            _call.setProperty(AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
            _call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
            _call.setOperationName(new QName("http://WebXml.com.cn/",
                    operationName));
            this.setRequestHeaders(_call);
            this.setAttachments(_call);
            Object resp = _call.invoke(params);
            System.out.println("resp=" + resp);
            if (resp instanceof RemoteException) {
                throw (RemoteException) resp;
            } else {
                this.extractAttachments(_call);
                try {
                    ret = (String[]) resp;
                } catch (Exception e) {
                    ret = (String[]) JavaUtils.convert(resp, String[].class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Arrays.asList(ret)=" + Arrays.asList(ret));
        return ret;
    }

    private static void setClassMapping(Call call) {
        Class beansf = BeanSerializerFactory.class;
        Class beandf = BeanDeserializerFactory.class;
        Class enumsf = EnumSerializerFactory.class;
        Class enumdf = EnumDeserializerFactory.class;
        Class arraysf = ArraySerializerFactory.class;
        Class arraydf = ArrayDeserializerFactory.class;
        Class simplesf = SimpleSerializerFactory.class;
        Class simpledf = SimpleDeserializerFactory.class;
        Class simplelistsf = SimpleListSerializerFactory.class;
        Class simplelistdf = SimpleListDeserializerFactory.class;
        QName qName = new QName("http://WebXml.com.cn/", "ArrayOfString");
        QName qName1 = new QName("http://www.w3.org/2001/XMLSchema", "string");
        QName qName2 = new QName("http://WebXml.com.cn/", "string");
        call.registerTypeMapping(String[].class, qName,
                new ArraySerializerFactory(qName1, qName2),
                new ArrayDeserializerFactory(), false);
    }

    private static void initOperationDesc() {
        operationDescArray = new OperationDesc[8];
        OperationDesc oper;
        ParameterDesc param;

        oper = new OperationDesc();
        oper.setName("getAreaString");
        oper.setReturnType(new QName("http://WebXml.com.cn/", "ArrayOfString"));
        oper.setReturnClass(String[].class);
        oper.setReturnQName(new QName("http://WebXml.com.cn/",
                "getAreaStringResult"));
        param = oper.getReturnParamDesc();
        param.setItemQName(new QName("http://WebXml.com.cn/", "string"));
        oper.setStyle(Style.WRAPPED);
        oper.setUse(Use.LITERAL);
        operationDescArray[1] = oper;

        oper = new OperationDesc();
        oper.setName("getTVstationString");
        param = new ParameterDesc(new QName("http://WebXml.com.cn/",
                "theAreaID"), ParameterDesc.IN, new QName(
                "http://www.w3.org/2001/XMLSchema", "int"), int.class, false,
                false);
        oper.addParameter(param);
        oper.setReturnType(new QName("http://WebXml.com.cn/", "ArrayOfString"));
        oper.setReturnClass(String[].class);
        oper.setReturnQName(new QName("http://WebXml.com.cn/",
                "getTVstationStringResult"));
        param = oper.getReturnParamDesc();
        param.setItemQName(new QName("http://WebXml.com.cn/", "string"));
        oper.setStyle(Style.WRAPPED);
        oper.setUse(Use.LITERAL);
        operationDescArray[3] = oper;

        oper = new OperationDesc();
        oper.setName("getTVchannelString");
        param = new ParameterDesc(new QName("http://WebXml.com.cn/",
                "theTVstationID"), ParameterDesc.IN, new QName(
                "http://www.w3.org/2001/XMLSchema", "int"), int.class, false,
                false);
        oper.addParameter(param);
        oper.setReturnType(new QName("http://WebXml.com.cn/", "ArrayOfString"));
        oper.setReturnClass(String[].class);
        oper.setReturnQName(new QName("http://WebXml.com.cn/",
                "getTVchannelStringResult"));
        param = oper.getReturnParamDesc();
        param.setItemQName(new QName("http://WebXml.com.cn/", "string"));
        oper.setStyle(Style.WRAPPED);
        oper.setUse(Use.LITERAL);
        operationDescArray[5] = oper;

        oper = new OperationDesc();
        oper.setName("getTVprogramString");
        param = new ParameterDesc(new QName("http://WebXml.com.cn/",
                "theTVchannelID"), ParameterDesc.IN, new QName(
                "http://www.w3.org/2001/XMLSchema", "int"), int.class, false,
                false);
        oper.addParameter(param);
        param = new ParameterDesc(
                new QName("http://WebXml.com.cn/", "theDate"),
                ParameterDesc.IN, new QName("http://www.w3.org/2001/XMLSchema",
                        "string"), String.class, false, false);
        param.setOmittable(true);
        oper.addParameter(param);
        param = new ParameterDesc(new QName("http://WebXml.com.cn/", "userID"),
                ParameterDesc.IN, new QName("http://www.w3.org/2001/XMLSchema",
                        "string"), String.class, false, false);
        param.setOmittable(true);
        oper.addParameter(param);
        oper.setReturnType(new QName("http://WebXml.com.cn/", "ArrayOfString"));
        oper.setReturnClass(String[].class);
        oper.setReturnQName(new QName("http://WebXml.com.cn/",
                "getTVprogramStringResult"));
        param = oper.getReturnParamDesc();
        param.setItemQName(new QName("http://WebXml.com.cn/", "string"));
        oper.setStyle(Style.WRAPPED);
        oper.setUse(Use.LITERAL);
        operationDescArray[7] = oper;
    }

    public static void main(String[] args) {
        TvProgramWsClient client = new TvProgramWsClient();
        client.invoke("getTVprogramString", new Object[] { 1, "2012-04-03",
                null });
    }
}
